import { Entity, Column, OneToMany, ManyToOne, JoinColumn } from "typeorm";

import type { INTEGER_PG, Contact, TonOrder, Voucher, TonSku, Book, MarketOrder, GemSku, AppDueSku, ContactGroup, BookMap, BookContactMap } from "qqlx-core";
import { ContactGateway, ContactGroupGateway } from "qqlx-core";

import { TransformerInteger } from "@lib/orm.transfor.number";
import { IdIntroSchema } from "@/alpha/ext.id.intro";

@Entity({ name: ContactGroupGateway.relations })
export class ContactGroupSchema extends IdIntroSchema implements ContactGroup {
  @OneToMany("ContactSchema", (s: Contact) => s.justContactGroup)
  joinContactList!: Contact[];
}

@Entity({ name: ContactGateway.relations })
export class ContactSchema extends IdIntroSchema implements Contact {
  @Column({ transformer: new TransformerInteger() })
  refContactGroupId!: INTEGER_PG;

  @ManyToOne("ContactGroupSchema", (s: ContactGroup) => s.joinContactList)
  @JoinColumn({ name: "refContactGroupId", referencedColumnName: "id" })
  justContactGroup!: ContactGroup;

  // ============ joinContactRelatived ============
  // ============ joinContactRelatived ============
  // ============ joinContactRelatived ============
  // ============ joinContactRelatived ============

  @OneToMany("BookSchema", (s: Book) => s.justContact)
  joinBookList!: Book[];

  @OneToMany("BookMapSchema", (s: BookMap) => s.justContact)
  joinBookMapList!: BookMap[];

  @OneToMany("BookContactMapSchema", (s: BookContactMap) => s.justContact)
  joinBookContactMapList!: BookContactMap[];

  @OneToMany("MarketOrderSchema", (s: MarketOrder) => s.justContact)
  joinMarketOrderList!: MarketOrder[];

  @OneToMany("GemSkuSchema", (s: GemSku) => s.justContact)
  joinGemSkuList!: GemSku[];

  @OneToMany("AppDueSkuSchema", (s: AppDueSku) => s.justContact)
  joinAppDueSkuList!: AppDueSku[];

  @OneToMany("TonOrderSchema", (s: TonOrder) => s.justContact)
  joinTonOrderList!: TonOrder[];

  @OneToMany("TonSkuSchema", (s: TonSku) => s.justContact)
  joinTonSkuList!: TonSku[];

  @OneToMany("VoucherSchema", (s: Voucher) => s.justContact)
  joinVoucherList!: Voucher[];
}
